package com.example.zytj.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.zytj.mapper.IMemberMapper;
import com.example.zytj.model.Member;
import com.example.zytj.model.dto.MemberDTO;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/member")
public class MemberController {
    @Resource
    private IMemberMapper mapper;

    @ApiOperation(value = "查找会员", notes = "多条件查询")
    @GetMapping
    public Map<String, Object> findMember(MemberDTO dto) {
        Map<String, Object> map = new HashMap<>();
        // 创建查询构造器
        QueryWrapper<Member> qw = new QueryWrapper<>();
        // 通用条件
        if (dto.getName() != null && dto.getName().length() > 0)
            qw.like("name", dto.getName());// 按姓名模糊查询
        if (dto.getCardNo() != null && dto.getCardNo().length() > 0)
            qw.eq("card_no", dto.getCardNo());
        if (dto.getPhone() != null && dto.getPhone().length() > 0)
            qw.eq("phone", dto.getPhone());
        if (dto.getStart() != null && dto.getStart().length() > 0)
            qw.ge("regdate", dto.getStart());
        if (dto.getEnd() != null && dto.getEnd().length() > 0)
            qw.le("regdate", dto.getEnd());
        // 其他条件(针对类别为学生的条件)
        if (dto.getCategory() != null && dto.getCategory() == 0) {
            if (dto.getDepartment() != null && dto.getDepartment().length() > 0)
                qw.eq("department", dto.getDepartment());
            if (dto.getProfession() != null && dto.getProfession().length() > 0)
                qw.eq("profession", dto.getProfession());
            if (dto.getGrade() != null && dto.getGrade().length() > 0)
                qw.eq("grade", dto.getGrade());
        }
        // 按主键排序
        qw.orderByAsc("id");
        List<Member> list = mapper.selectList(qw);
        map.put("data", list);
        return map;
    }

    @ApiOperation(value = "添加会员", notes = "一次添加一个")
    @PostMapping
    public int add(Member m) {
        return mapper.insert(m);
    }

    @ApiOperation(value = "批量添加会员", notes = "从excel导入")
    @PostMapping("batch")
    public int batchAdd(List<Member> list) {
        int n = 0;
        for (Member m : list) {
            n += mapper.insert(m);
        }
        // 返回的是添加成功的条数
        return n;
    }

    @ApiOperation(value = "修改会员信息", notes = "修改基本信息，卡号和入会时间不能修改")
    @PutMapping
    public int edit(Member m) {
        return mapper.updateById(m);
    }

    @ApiOperation(value = "删除会员", notes = "单个删除，逻辑删除，修改备注")
    @DeleteMapping
    public int del(int id) {
        // 尽量不用物理删除，困难会造成级联问题
        // mapper.deleteById(id);
        // 逻辑删除，修改备注
        Member m = new Member();
        m.setId(id);
        m.setRemark("已删除");
        return mapper.updateById(m);
    }

    @ApiOperation(value = "批量删除会员", notes = "批量逻辑删除，修改备注")
    @DeleteMapping("batch")
    public int batchDel(int[] ids) {
        int n = 0;
        for (int id : ids) {
            Member m = new Member();
            m.setId(id);
            m.setRemark("已删除");
            n += mapper.updateById(m);
        }
        //返回的是删除成功条数
        return n;
    }

    @ApiOperation(value = "重置卡",notes = "防止有人出去忘记刷卡，可以找管理员重置")
    @PutMapping("reset")
    public int reset(int id){
        Member m = new Member();
        m.setId(id);
        m.setStatus(0);//status归0表示出去
        return mapper.updateById(m);
    }
}
